package com.bagus.mediapipefacedetection;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.bagus.mediapipefacedetection.utils.ImageUtil;

/**
 * @Description TODO
 * @Author yeqing
 * @Date 05-12-2023 周五 18:07
 */
public class MainActivity2 extends Activity {
    ImageView ivOriginal,ivCapture;
    Button capImage;
    private static final int CAMERA_DETECT = 3;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ivOriginal = findViewById(R.id.iv_iamge_original);
        ivCapture = findViewById(R.id.iv_iamge_capture);
        capImage = findViewById(R.id.capImage);
        capImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity2.this, MainActivity.class);
                startActivityForResult(intent,CAMERA_DETECT);
            }
        });
    }

    private BitmapDrawable decodeImage(String path, ImageView imageView) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        Bitmap bm = BitmapFactory.decodeFile(path, options);
        android.graphics.Bitmap.Config bitmapConfig = bm.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bm = bm.copy(bitmapConfig, true);
        int width = bm.getWidth();
        int height = bm.getHeight();
        // By ratio scale
        float aspectRatio = bm.getWidth() / (float) bm.getHeight();

        final int MAX_SIZE = 512;
        int newWidth = MAX_SIZE;
        int newHeight = MAX_SIZE;
        float resizeRatio = 1;
        newHeight = Math.round(newWidth / aspectRatio);
        if (bm.getWidth() > MAX_SIZE && bm.getHeight() > MAX_SIZE) {
            bm = getResizedBitmap(bm, newWidth, newHeight);
            resizeRatio = (float) bm.getWidth() / (float) width;
        }
        imageView.setImageBitmap(bm);
        return new BitmapDrawable(getResources(), bm);
    }

    private Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
        return resizedBitmap;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if(requestCode == CAMERA_DETECT){
                //显示原图和头部图片
                String image1 = ImageUtil.getSaveImage(this,0);
                String image2 = ImageUtil.getSaveImage(this,1);

                if (!TextUtils.isEmpty(image1) && !TextUtils.isEmpty(image2)) {
                    decodeImage(image1,ivOriginal);
                    decodeImage(image2,ivCapture);
                } else {
                    Toast.makeText(getApplicationContext(), "肤测失败", Toast.LENGTH_LONG).show();
                }
            }

        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
        }
    }

}
